/*
 * This file is part of finding-duplicates.
 *
 * finding-duplicates is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * finding-duplicates is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with finding-duplicates.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.xemantic.duplicates;

/**
 *
 * <p>
 * Created on Jul 13, 2008
 *
 * @author hshsce
 * @version $Id$
 */
public abstract class Permutator {

	private final String m_name;

	private long m_count = 0;

	private long m_duration = 0;

	private final int m_maxElementCount;

	public Permutator(final String name, final int maxElementCount) {
		if (maxElementCount <= 0) {
			throw new IllegalArgumentException("maxElementCount must be positive integer");
		}
		m_name = name;
		m_maxElementCount = maxElementCount;
	}

	public void run() {
		final long startTime = System.currentTimeMillis();
		for (int n = 1; n <= m_maxElementCount; n++) {
			final int[] a = new int[n];
			permutateWithRepetitions(a, 0);
		}
		m_duration = System.currentTimeMillis() - startTime;
	}

	public void permutateWithRepetitions(final int[] a, final int pos) {
		for (int i = 1; i <= a.length; i++) {
			a[pos] = i;
			if (pos != (a.length -1)) {
				permutateWithRepetitions(a, pos + 1);
			} else {
				m_count++;
				handle(a);
			}
		}
	}

	public long getDuration() {
		return m_duration;
	}

	public String getName() {
		return m_name;
	}

	protected abstract void handle(final int[] a);

}
